package serviceImpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.BaseDAO;
import DTO.Pl;
import pageUtil.PageB;
import util.DBConnection;
import util.DBConnection.CallBack;

public class PlDAO implements BaseDAO<Pl> {

	@Override
	public boolean add(Pl pl, String sql) throws Exception {
		Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,
        		pl.getSpid(),
        		pl.getPlxx(),
        		pl.getPlsj(),
        		pl.getPlrid(),
        		pl.getStatus()
        );
        if(conn!=null){
            conn.close();
        }
        return boo;
	}

	@Override
	public boolean delete(Pl t) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Pl t, String sql) throws Exception {
		Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,
                t.getId()
        );
        if(conn!=null){
            conn.close();
        }
        return boo;
	}

	@Override
	public List<Pl> findAll(int currentPage, int pageSize, String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Pl> findAll(String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pl findById(String id) throws Exception {
		String sql = "select a.id,a.name,a.photo,a.money,b.lbname as lb from merchaned a,lb b where a.lb = b.lbcs and a.id = ?";
        return DBConnection.queryOne(new CallBack<Pl>() {
            @Override
            public Pl getData(ResultSet rs) {
            	Pl pl = null;
                try {
                    if(rs.next()){
                    	pl = new Pl();
                    	pl.setId(rs.getInt("id"));
                    	pl.setPlxx(rs.getString("plxx"));
                    	pl.setSpid(rs.getString("spid"));
                    	pl.setPlsj(rs.getString("plsj"));
                    	pl.setPlrid(rs.getString("plrid"));
                    	pl.setStatus(rs.getInt("status"));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!= null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return pl;
            }


        }, sql, id);
	}

	@Override
	public Pl findByPropert(Pl t) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int dataCount(String sql) throws Exception {
		return DBConnection.queryOne(new CallBack<Integer>() {
            @Override
            public Integer getData(ResultSet rs) {
                int count = 0;
                try {
                    if(rs.next()){
                        count = rs.getInt(1);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return count;
            }
        }, sql);
	}

	@Override
	public List<Pl> findByPage(PageB<Pl> bean, String sql) throws SQLException {
		int start = (bean.getCurrentPage()-1)*bean.getPageSize();
        return DBConnection.queryList(new CallBack<Pl>() {

            @Override
            public List<Pl> getDatas(ResultSet rs) {
                List<Pl> list = new  ArrayList<Pl>();
                Pl pl = null;
                try {
                    while(rs.next()){
                    	pl = new Pl();
                    	pl.setId(rs.getInt("id"));
                    	pl.setPlxx(rs.getString("plxx"));
                    	pl.setSpid(rs.getString("spname"));
                    	pl.setPlsj(rs.getString("plsj"));
                    	pl.setPlrid(rs.getString("username"));
                    	pl.setStatus(rs.getInt("status"));
                    	list.add(pl);
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

                return list;
            }

        }, sql, start,bean.getPageSize());
    }
}
